import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

let routes = [
    {
        path: '/',
        redirect: '/login',
    },
      // 学生端
      {
        path: '/student',
        name: 'Student',
        component: () => import("../layout/student"),
        children: [
            {
                path: 'home',
                name: 'StudentHome',
                component: () => import("../views/student/home")
            },
            {
                path: 'tkxx',
                name: 'tkxx',
                component: () => import("../views/student/tkxx")
            },
            {
                path: 'tmlx',
                name: 'tmlx',
                component: () => import("../views/student/tmlx")
            },
            {
                path: 'znddtyd',
                name: 'znddtyd',
                component: () => import("../views/student/znddtyd")
            }
            ,
            {
                path: 'dtjl',
                name: 'dtjl',
                component: () => import("../views/student/dtjl")
            },
            {
                path: 'mnks',
                name: 'mnks',
                component: () => import("../views/student/mnks")
            },
            {
                path: 'person',
                name: 'person',
                component: () => import("../views/student/person")
            },
           
        ]
    },
      // 教师端
      {
        path: '/teacher',
        name: 'teacher',
        component: () => import("../layout/teacher"),
        children: [
            {
                path: 'home',
                name: 'teacherHome',
                component: () => import("../views/teacher/home")
            },
            {
                path: 'kyxx',
                name: 'kyxx',
                component: () => import("../views/teacher/kyxx")
            },
            {
                path: 'xsbj',
                name: 'xsbj',
                component: () => import("../views/teacher/xsbj")
            },
            {
                path: 'tktk',
                name: 'tktk',
                component: () => import("../views/teacher/tktk")
            }
            ,
            {
                path: 'zndxx',
                name: 'zndxx',
                component: () => import("../views/teacher/zndxx")
            },
            {
                path: 'xsdtsj',
                name: 'xsdtsj',
                component: () => import("../views/teacher/xsdtsj")
            },
            {
                path: 'person',
                name: 'person',
                component: () => import("../views/teacher/person")
            },
        ]
    },
  
    // 后台管理
    {
        path:'/manage',
        name:'Manage',
        component:() => import("../layout/manage"),
        children:[
            {
                path: 'home',
                name: 'Home',
                component: () => import("../views/manage/home")
            },
            {
                path: 'user',
                name: 'User',
                component: () => import("../views/manage/user")
            },
            {
                path: 'teacher',
                name: 'teacher',
                component: () => import("../views/manage/teacher")
            },
            {
                path: 'person',
                name: 'person',
                component: () => import("../views/manage/person")
            },
            
            
    ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import("../views/login")
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import("../views/register")
    },
    {
        path: '/registerT',
        name: 'RegisterT',
        component: () => import("../views/registerT")
    },
]


const createRouter = () => new VueRouter({
    mode: 'history',
    routes: routes
})

const router = createRouter()

let user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
resetRouter(user.permission)

//写一个重置路由的方法，切换用户后，或者退出时清除动态加载的路由
export function resetRouter(permissions) {
    if (permissions) {
        const newRouter = createRouter()
        router.matcher = newRouter.matcher // 新路由实例matcer，赋值给旧路由实例的matcher，（相当于replaceRouter）
        if (!router.getRoutes().find(v => v.name === 'Manage')) {
            router.addRoute(getPermissions(permissions))
        }
    }

}

function getPermissions(permissions) {
    let manage = {
        path: '/manage', name: 'Manage', component: () => import("../layout/manage"),
        children: [
            {path: 'person', name: '个人信息', component: () => import("../views/manage/person")}
        ]
    }
    // 设置路由菜单
    if (permissions) {
        permissions.forEach(item => {
            manage.children.push({
                path: "/manage" + item.path,
                name: item.name,
                component: () => import("../views/manage" + item.path)
            });
        })
    }
    return manage
}


// vueRouter.beforeEach((to, from, next) => {
//     // 判断要去的路由界面是不是要鉴权
//     if (to.path.includes('manage')) {
//         // 查看是否登陆
//         if (!user.id) {
//             // 没登录的做处理
//             next({
//                 path: '/login'
//             })
//         } else {
//             // 登陆的正常跳
//             next()
//         }
//     } else {
//         // 不需要鉴权的正常跳
//         next()
//     }
// })

export default router
